import os, sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))  # 添加导包路径
import uvicorn
from fastapi import FastAPI, File, UploadFile
from server.request import *

app = FastAPI()

# 接收reqeuest包发的请求
# 判断文件类型, 将文件保存在本地
# 移动文件位置
@app.get("/")
async def read_root():
    return {"Hello": "World"}

@app.post("/upfile")
async def receive_pdf(file: UploadFile = File(...)):
    """
    :param file:file对象
    :return:
    """
    print(file.filename)
    up_name = file.filename.split('.')[0]
    print(up_name)
    try:
        file_indexid = await upload(file)  # 数据库索引id
        print(file_indexid)
        task_item_dict = await get_task_item(up_name, file_indexid)  # 项目名,单号名 字典
        pathlist = await create_filepath(file.filename, task_item_dict)
        bool1 = await store_file(file, pathlist)
    except:
        return '{}存储过程失败'.format(file.filename)
    if bool1:
        return {'code': 200, 'msg': 'ok'}
    else:
        return {'code': 500, 'msg': 'error'}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app="main:app", host="127.0.0.1", port=8000, reload=True)
